/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vienthonga.hibernate.dao;

import com.vienthonga.hibernate.pojo.TParam;
import com.vienthonga.hibernate.pojo.TProduct;
import com.vienthonga.hibernate.utils.HibernateUtil;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author DoanHa
 */
public class ShowProductDAO {

    public List<TProduct> getMobilePhoneByManufacturer(String manufacturerID, int start, int itemsOnPage) {
        List<TProduct> mobilePhoneProducts = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p left join fetch p.TSupplier "
                    + "left join fetch p.TCategory "
                    + "where p.TSupplier.SId = :SId and p.TCategory.catId = :catID";
            //String hqlQuery = "from TProduct";
            Query query = session.createQuery(hqlQuery);
            query.setString("catID", "P");
            query.setString("SId", manufacturerID);
            query.setFirstResult(start);
            query.setMaxResults(itemsOnPage);
            mobilePhoneProducts = query.list();

            return mobilePhoneProducts;
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            session.close();
        }
        return mobilePhoneProducts;

    }

    public static int GetItemsOnPage() {
        int itemsOnPage = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TParam param where param.paramName = :ParamName ";

            Query query = session.createQuery(hqlQuery);
            query.setString("ParamName", "PAGING");
            TParam param = (TParam) query.uniqueResult();

            itemsOnPage = Integer.parseInt(param.getParamValue());
            return itemsOnPage;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            session.close();
        }
        return itemsOnPage;
    }

    public static int GetItemsTotal(String manufacturerID) {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p left join fetch p.TSupplier "
                    + "left join fetch p.TCategory "
                    + "where p.TSupplier.SId = :SId and p.TCategory.catId = :catID";
            //String hqlQuery = "from TProduct";
            Query query = session.createQuery(hqlQuery);
            query.setString("catID", "P");
            query.setString("SId", manufacturerID);
            numberOfItem = query.list().size();

            return numberOfItem;
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            session.close();
        }
        return numberOfItem;
    }

    public static TProduct getMobilephoneDetail(String pId) {
        TProduct product = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PId = :P_ID ";

            Query query = session.createQuery(hqlQuery);
            query.setString("P_ID", pId);

            product = (TProduct) query.uniqueResult();

            return product;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            session.close();
        }
        return product;
    }

    /*----------------show san pham moi--------------*/
    public static List<TProduct> getNewMobilephone() {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PHot = :P_HOT ";

            Query query = session.createQuery(hqlQuery);
            query.setInteger("P_HOT", 0);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static List<TProduct> getNewMobilephone(int start,int itemsOnPage) {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PHot = :P_HOT ";

            Query query = session.createQuery(hqlQuery);
            query.setInteger("P_HOT", 0);
            query.setFirstResult(start);
            query.setMaxResults(itemsOnPage);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static int GetNewMobilephoneTotal() {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PHot = :P_HOT ";

            Query query = session.createQuery(hqlQuery);
            query.setInteger("P_HOT", 0);
            numberOfItem = query.list().size();

            return numberOfItem;
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            session.close();
        }
        return numberOfItem;
    }
    /*----------------show san pham thong dung--------------*/
    public static List<TProduct> getNormalMobilephone() {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice <= 200000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(0);   // lay ra 4 san pham de show len giao dien
            query.setMaxResults(4);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static List<TProduct> getNormalMobilephone(int start,int itemsOnPage) {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice <= 200000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(start);   // lay ra 4 san pham de show len giao dien
            query.setMaxResults(itemsOnPage);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static int getNormalMobilephoneTotal() {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice <= 200000 ";

            Query query = session.createQuery(hqlQuery);
            numberOfItem = query.list().size();

            return numberOfItem;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            
        } finally {
            session.close();
        }
       return numberOfItem;
    }
    
    /*----------------show san pham trung cap--------------*/
    public static List<TProduct> getMediumMobilephone() {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 2000000 and 4999000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(0);   // lay ra 4 san pham de show len giao dien
            query.setMaxResults(4);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static List<TProduct> getMediumMobilephone(int start,int itemsOnPage) {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 2000000 and 4999000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(start);  
            query.setMaxResults(itemsOnPage);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static int getMediumMobilephoneTotal() {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 2000000 and 4999000 ";

            Query query = session.createQuery(hqlQuery);
            numberOfItem = query.list().size();

            return numberOfItem;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());

        } finally {
            session.close();
        }
       return numberOfItem;
    }

    /*----------------show san pham cao cap--------------*/
    public static List<TProduct> getHighMobilephone() {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 5000000 and 8999000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(0);   // lay ra 4 san pham de show len giao dien
            query.setMaxResults(4);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static List<TProduct> getHighMobilephone(int start,int itemsOnPage) {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 5000000 and 8999000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(start);
            query.setMaxResults(itemsOnPage);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static int getHighMobilephoneTotal() {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice between 5000000 and 8999000 ";

            Query query = session.createQuery(hqlQuery);
            numberOfItem = query.list().size();

            return numberOfItem;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());

        } finally {
            session.close();
        }
        return numberOfItem;
    }

    /*----------------show san pham sieu cap--------------*/
    public static List<TProduct> getSuperMobilephone() {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice >= 9000000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(0);   // lay ra 4 san pham de show len giao dien
            query.setMaxResults(4);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static List<TProduct> getSuperMobilephone(int start,int itemsOnPage) {
        List<TProduct> products = new ArrayList<TProduct>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice >= 9000000 ";

            Query query = session.createQuery(hqlQuery);
            query.setFirstResult(start);
            query.setMaxResults(itemsOnPage);

            products = query.list();

            return products;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            return null;
        } finally {
            session.close();
        }

    }

    public static int getSuperMobilephoneTotal() {
        int numberOfItem = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            // lay thong tin dien thoai
            String hqlQuery = "from TProduct p where p.PUnitPrice >= 9000000 ";

            Query query = session.createQuery(hqlQuery);
            numberOfItem = query.list().size();

            return numberOfItem;

        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
         
        } finally {
            session.close();
        }
        return numberOfItem;
    }
}
